package com.sservice.web.controller.pc;

import com.sservice.common.Constants;
import com.sservice.model.dao.MsdsMapper;
import com.sservice.model.vo.MsdsVo;
import com.sservice.service.service.ISearchRecordService;
import com.sservice.base.entity.dto.BaseResult;
import com.sservice.base.entity.dto.ListResult;
import com.sservice.base.entity.dto.Pagination;
import com.sservice.model.entity.Msds;
import com.google.zxing.WriterException;
import com.sservice.web.common.MatrixToImageWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@Controller
@RequestMapping("/msds")
public class MsdsPcController {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsdsMapper msdsMapper;
    @Autowired
    private ISearchRecordService iSearchRecordService;


    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<Msds> get(Msds arg, HttpSession session) {
        Msds service_res = msdsMapper.selectByPrimaryKey(arg.getId());
        BaseResult<Msds> res = new BaseResult<Msds>(true, service_res);
        return res;
    }

    /**
     * 微信、支付宝二维码
     *
     * @param arg
     * @return
     */
    @RequestMapping(value = "/msds/getOrderQcode", method = RequestMethod.GET)
    public void getOrderQcode(Msds arg, HttpServletResponse response) throws IOException, WriterException {
        MatrixToImageWriter.createRqCode(Constants.domain + "/api/msds/choosePayWay", 400, 400, response.getOutputStream());
        response.flushBuffer();
    }

    @RequestMapping(value = "/choosePayWay", method = RequestMethod.GET)
    public String choosePayWay(Msds arg, HttpServletRequest request) throws IOException, WriterException {
        String agent = request.getHeader("user-agent");
        if (agent != null) {
            agent = agent.toLowerCase();
            if (agent.indexOf("micromessenger") > 0) {
                return "redirect:/api/wx/mp/auth";
            } else if (agent.indexOf("alipayclient") > 0) {
                logger.debug("阿里支付");
            }
        }
        return null;


    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult query(MsdsVo arg, HttpServletRequest request) {
        List<Msds> list = msdsMapper.queryByPage(arg);
        if (arg.getKeyword() != null && !"".equals(arg.getKeyword())) {
            iSearchRecordService.log(arg.getKeyword(), request, "1");
        }
        int total = msdsMapper.queryCount(arg);
        ListResult res = new ListResult(true, list);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        res.setPage(page);
        return res;
    }
}
